package org.store.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.store.domain.Product;
import org.store.service.ProductService;

import javax.ws.rs.*;
import java.util.List;

@Path("/product")
@Produces("application/json")
public class ProductResource {

    @Autowired
    private ProductService productService;

    @GET
    @Path("/{id}")
    public Product getById(@PathParam("id") Long id) throws Exception {
        try {
            return productService.getProductById(id);
        } catch (NotFoundException e) {
            throw new NotFoundException();
        }
    }

    @GET
    @Path("/title/{title}")
    public List<Product> getByTitle(@PathParam("title") String title) throws Exception {
        return productService.getProductByTitle(title);
    }

    @GET
       @Path("/title/{categoryId}")
       public List<Product> getByCategoryId(@PathParam("categoryId") long categoryId) throws Exception {
           return productService.getProductsByCategoryId(categoryId);
       }

    @POST
    public void addProduct(Product product) throws Exception {
        productService.saveProduct(product);
    }

    @DELETE
    public void deleteProduct(Product product) throws Exception {
        productService.delete(product);
    }

    @GET
    public List<Product> getProducts(@QueryParam("start") Integer start,
                                     @QueryParam("limit") Integer limit) throws Exception {
        return productService.getAll(start, limit);
    }
}